package tuiLayer;
import java.util.Scanner;

import controlLayer.*;
import modelLayer.*;
import tuiLayer.*;

public class ItemUI {
    MainUI mainUI;
    ItemController itemCtrl = new ItemController();
    String errorMessage = "Incorrect command!";
    ItemContainer itemCont = ItemContainer.getInstance();
    Scanner keyboard = new Scanner(System.in);
    
    public ItemUI(){        
        writeItemMenu();        
    }
    
    

    public void writeItemMenu(){
        
        
        System.out.println("");
        System.out.println("");
        System.out.println("***\fStart item Menu***");
        System.out.println(" (1) Add Item");
        System.out.println(" (2) Find Item");
        System.out.println(" (3) Update Item");
        //System.out.println(" (4) Delete Item");
        System.out.println(" (5) Return to main menu");
        
        String choice = getString();
        
        switch(choice){
        case "1" :
            addItem();
            writeItemMenu();
            break;
        
        case "2" :
            findItem();
            writeItemMenu();
            break;  
            
        case "3" :
            updateItem();
            writeItemMenu();
            break;
        /*
            case "4" :
            deleteItem();
            writeItemMenu();
            break;
        */
       case "5":
            mainUI= new MainUI();
            break;
       
        default:      
            System.out.println(errorMessage);
            pressEnter();
            writeItemMenu();
        }
        }
    
public void addItem(){
    String name=""; 
    double price=-1; 
    int barcode=-1; 
    int quantity=-1;
    String location="";
    boolean unic;
    
    
     do{
            unic=true;    
            Item i=null;
            String s="";
            do{
            System.out.println("Type barcode: ");
            s=getString();
            if(s.equals("exit")) writeItemMenu();
            try{
            barcode = Integer.parseInt(s);
            }catch(NumberFormatException e){
                System.out.println("Not a number ! Try again or type exit to quit");
                s="";
            }
            }while(s.equals("exit"));
            i=itemCtrl.findItem(barcode);
            if(i!=null){
            unic=false;
            System.out.println("Barcode already in the system.\nTry again");
            }
    }while(!unic);
     //check for name
     do{
     System.out.println("Type the name of Item or type exit to quit: ");    
     name = getString();
     if(name.equals("exit")) writeItemMenu();
     }while(name.equals(""));
     //check for price
     System.out.println("Type the price of the Item");
     String priceString="";
     do{
     priceString=getString();
     if(priceString.equals("exit")) writeItemMenu();
     try{
     price = Double.parseDouble(priceString);
     }
     catch(NumberFormatException e){
     System.out.println("Not a number ! Try again or type exit to quit");
     priceString="";
     }
     }while(priceString.equals(""));
     //check for quantity
     System.out.println("Type the quantity of the Item");
     String quantityString="";
     do{
         quantityString=getString();
         if(quantityString.equals("exit")) writeItemMenu();
         try{
         quantity =  Integer.parseInt(quantityString);
         }catch(NumberFormatException e){
             System.out.println("Not a number");
             quantityString="";
         }
         }
     while(quantityString.equals(""));
     //check for location
     
     System.out.println("Type the location of the Item");
     location= getString();
     if(location.equals("exit")) writeItemMenu();
     Item item = new NormalItem(name,price,barcode,quantity,location);
        itemCont.addNormalItem(item);
        
        System.out.println("Product was added to the database");
    }

public void findItem(){
    int barcode=0;
    Item i;
    String barcodeString="";
    do{
    System.out.println("Type barcode to find Item");
    barcodeString=getString();
    if(barcodeString.equals("exit")) writeItemMenu();
    try{
    barcode = Integer.parseInt(barcodeString);
    }catch(NumberFormatException e){
        System.out.println("Not a number ! Try again or type exit to quit");
        barcodeString="";
    }
    }while(barcodeString.equals(""));
    i = itemCtrl.findItem(barcode);
    if(i==null){
        System.out.println("Item is not found");
    }else{
        System.out.println("The name of Item :" + i.getName());
        System.out.println("The price of Item :" + i.getPrice());
        System.out.println("The quantity of Item :" + i.getQuantity());
    }
    }

public void updateItem(){
   
    String name, barcodeString="";
    double price = 0;
    int quantity = 0,barcode=0;
    Item i=null;
    
    boolean updatedSubMenu=true;
    do{
    System.out.println("Type barcode of Item you want update");
    barcodeString=getString();
    if(barcodeString.equals("exit")) writeItemMenu();
    try{
    barcode = Integer.parseInt(barcodeString);
    }catch(NumberFormatException e){
        System.out.println("Not a number ! Try again or type exit to quit");
    }
    }while(barcodeString.equals(""));
    i = itemCtrl.findItem(barcode);
    
    if(i== null){
        System.out.println("The Item not found");
        
    }else{
        
        System.out.println("Information about Item :");
        System.out.println("name :" + i.getName());
        System.out.println("price :" + i.getPrice());
        System.out.println("quantity :" + i.getQuantity());
        System.out.println("barcode:" + i.getBarcode());
        System.out.println("**************");
    
        while(updatedSubMenu!=true);
    {
        
        System.out.println("");
        System.out.println("**Update Menu**");
        System.out.println("(1) Update name");
        System.out.println("(2) Update price");
        //System.out.println("(3) Update barcode");
        System.out.println("(4) Update quantity");
        System.out.println("(5) Cancel and go back to Item Menu");
        
        int choice= Integer.parseInt(getString());
        switch(choice){
        
        case(1):
            System.out.println("Type name you prefered or press enter to quit:");
            name = getString();
            if(name.equals("exit")) writeItemMenu();
            i.setName(name);
            System.out.println("System changed the name");          
            break;
         
        case(2):
            String priceString="";
            do{
            System.out.println("Type price you want to change :");
            priceString=getString();
            try{
            price = Double.parseDouble(priceString);
            }catch(NumberFormatException e){
            System.out.println("Not a number ! Try again or type exit to quit");
            priceString="";
            }
            }while(priceString.equals("exit"));
            i.setPrice(price);
            System.out.println("System changed the price");
            break;
           /*
        case(3):
            System.out.println("Type new barcode");
            barcode = Integer.parseInt(getString());
            i.setBarcode(barcode);
            System.out.println("System changed the barcode");
            break;
            */
        case(4):
            String quantityString="";
        do{
            System.out.println("Type quantity of Item");
            quantityString=getString();
            try{
            quantity = Integer.parseInt(quantityString);
            }catch(NumberFormatException e){
            System.out.println("Not a number ! Try again or type exit to quit");
            quantityString="";
            }
        }while(quantityString.equals("exit"));
            i.setQuantity(quantity);
            System.out.println("System changed the quantity");
            break;
            
        case(5):
            writeItemMenu();
        
           
        break;
        default:
            System.out.println("Wrong command try again");
            pressEnter();   
        }
        }
    }
    }

/*
public void deleteItem(){
    
    Item i =null;
    int barcode;
    System.out.println("Type barcode of Item you want delete");
    barcode = Integer.parseInt(getString());
     i=itemCtrl.findItem(barcode);
    
    
    
    if(i==null){
        System.out.println("System not found Item");
    }else{
        if(i instanceof NormalItem){
            System.out.println("Item : " + i.getName() + " was deleted");
            itemCont.getInstance().removeNormalItem(i);
        }
        
    }
}
    */
    
    private void pressEnter(){
           Scanner scanner=new Scanner(System.in);
           System.out.println("Press enter to continue.");
           scanner.nextLine(); 
}
    private String getString(){
            String startChoice ;
            startChoice = keyboard.next();
            String choice = startChoice.trim();
            return choice;
}

    private String getStringForNames(){
        String startChoice = "";
        startChoice = keyboard.nextLine();
        return startChoice;
    }
}
